package com.ews.onlineexchange.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.app.entity.BulletinStat;
import com.ews.onlineexchange.controller.AgentServiceList;
import com.ews.onlineexchange.controller.QualificationClassList;
import com.ews.onlineexchange.model.AgentService;
import com.ews.onlineexchange.model.QualificationClass;

@Stateless
public class QualificationClassService {
		
	@Inject
	EntityManager entityManager;
	
	public QualificationClass find(String id) {
		if (StringUtils.isBlank(id))
			return null;
		QualificationClass qc = entityManager.find(QualificationClass.class, id);
		qc.getLevelOptions().size();
		qc.getRootSectors().size();
		return qc;
	}

	private static final String FIND_QUALIFICATION_CLASS_ID_EQL = ""
			+ "	SELECT		q"
			+ "	FROM		QualificationClass q"
			+ "	WHERE		q.name = :name	";
	
	private static final String GET_QUALIFICATIONlEVEL_EQL = ""
			+"SELECT level  "
			+" FROM  QualificationLevel level"
			+" WHERE level.qualificationClass.id =:classId ";
	public QualificationClass findQualificationClass(String name) {
		Query query = entityManager.createQuery(FIND_QUALIFICATION_CLASS_ID_EQL);
		query.setParameter("name", name);
		List<QualificationClass> resultList = query.getResultList();
		if (resultList.size() > 0) {
			return  resultList.get(0);
		}
			
		return null;
	}
	
	private static final String IS_NAME_EXIST_EQL = ""
			+"SELECT q  "
			+" FROM  QualificationClass q"
			+" WHERE q.name =:name "
			+" AND q.delFlag = 0";
	public boolean findQualificationClassByName(String name) {
		Query query = entityManager.createQuery(IS_NAME_EXIST_EQL);
		query.setParameter("name", name);
		List<QualificationClass> resultList = query.getResultList();
		if (resultList.size() > 0) {
			return  true;
		}
			
		return false;
	}
	@Inject
	QualificationClassList qualificationClassList;
	@Inject
	AgentServiceList agentServiceList;
	@Inject
	BulletinService bulletinService;
	public List<BulletinStat> QualificationStat(Date startDate,Date endDate){
		qualificationClassList.refresh();
		qualificationClassList.setMaxResults(null);
		agentServiceList.refresh();
		agentServiceList.setMaxResults(null);
		List<QualificationClass> classes = qualificationClassList.getResultList();
		List<AgentService> services = agentServiceList.getResultList();
		List<BulletinStat> bulletinStats = new ArrayList<>();
		classes.stream().forEach(qclass->{
			BulletinStat stat = new BulletinStat();
			stat.setDataStr(qclass.getName());
			List resultList = bulletinService.getBulletinCountByQualificationClass(startDate,endDate,qclass.getId());
			stat.setCount(resultList.size());
			BigDecimal totalPrice = new BigDecimal(0);
			Integer totalAppliedCount = 0;
			for (Object object : resultList) {
				Object[] array = (Object[])object;
				BigDecimal price = (BigDecimal)array[0] ;
				if(price == null) price = new BigDecimal(0);
				Integer appliedCount = (Integer)array[1];
				totalPrice = totalPrice.add(price);
				totalAppliedCount += appliedCount;
			}	
			stat.setPriceCount(totalPrice);
			stat.setAppliedCount(totalAppliedCount);
			bulletinStats.add(stat);
		});
 		
		services.stream().forEach(service->{
			BulletinStat stat = new BulletinStat();
			stat.setDataStr(service.getName());
			List resultList = bulletinService.getBulletinCountByAgentService(startDate,endDate,service.getId());
			stat.setCount(resultList.size());
			BigDecimal totalPrice = new BigDecimal(0);
			Integer totalAppliedCount = 0;
			for (Object object : resultList) {
				Object[] array = (Object[])object;
				BigDecimal price = (BigDecimal)array[0] ;
				if(price == null) price = new BigDecimal(0);
				Integer appliedCount = (Integer)array[1];
				totalPrice = totalPrice.add(price);
				totalAppliedCount += appliedCount;
			}	
			stat.setPriceCount(totalPrice);
			stat.setAppliedCount(totalAppliedCount);
			bulletinStats.add(stat);
		});
		
		return bulletinStats;
	}
	
}
